package com.demo2do.jit;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


/**
 * Created by Wilson on 2015/5/9.
 */

public class LoadingDialog extends Dialog {

    private String message;

    private int layoutId;

    public static int THEME = R.style.LoadingDialog;

    private TextView messageTextView;

    private ProgressBar progressBar;

    public LoadingDialog(Context context, String message){
        this(context, message, R.layout.dialog_loading);
    }

    public LoadingDialog(Context context, String message, int layoutId) {
        super(context , THEME);
        this.message = message;
        this.layoutId = layoutId;
    }

    public static LoadingDialog show(Context context, String message) {
        return show(context, message, false, null);
    }

    public static LoadingDialog show(Context context, String message, boolean cancelable) {
        return show(context, message, cancelable, null);
    }

    public static LoadingDialog show(Context context, String message, boolean cancelable, OnCancelListener cancelListener) {
        LoadingDialog dialog = new LoadingDialog(context, message);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show();
        return dialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(this.layoutId);
        messageTextView = (TextView)findViewById(R.id.messageTextView);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        messageTextView.setText(message);
    }

    public void changeMessage(String string){
        changeMessage(string, true);
    }

    public void changeMessage(String string, boolean showProgressBar) {
        messageTextView.setText(string);
        progressBar.setVisibility(showProgressBar ? View.VISIBLE : View.GONE);
    }

}
